/**
 * 
 */
package org.vectrics.recruiter.struts;

import org.apache.ecs.html.A;
import org.apache.ecs.html.Div;
import org.apache.ecs.html.Font;
import org.apache.ecs.html.IMG;
import org.apache.ecs.html.Input;
import org.vectrics.common.html.Cell;
import org.vectrics.common.html.Row;
import org.vectrics.common.html.Table;
import org.vectrics.common.util.StringUtils;
import org.vectrics.provider.CoreServiceLocator;
import org.vectrics.struts.VectricsTagSupport;
import org.vectrics.user.Person;

/**
 * @author pgoette
 * Genera un tag que lista las personas con un cuadrito
 */
public class PersonPickerTag extends VectricsTagSupport {

	private static final long serialVersionUID = -714269140971841860L;

	private Cell labelCell = null;
	private Cell valueCell = null;
	private Div valueDiv = null;
	private Div labelDiv = null;
	private String contextPath;
	private String popupLeft;
	private String popupTop;
	private String caption;
	private String paramName;
	private String style;
	private String styleClase;

	private String remoteMethod;

	/**
	 * @return the styleClase
	 */
	public String getStyleClase() {
		return styleClase;
	}

	/**
	 * @param styleClase the styleClase to set
	 */
	public void setStyleClase(String styleClase) {
		this.styleClase = styleClase;
	}

	/**
	 * @return the contextPath
	 */
	public String getContextPath() {
		return contextPath;
	}

	/**
	 * @param contextPath the contextPath to set
	 */
	public void setContextPath(String contextPath) {
		this.contextPath = contextPath;
	}

	/**
	 * @return the popupLeft
	 */
	public String getPopupLeft() {
		return popupLeft;
	}

	/**
	 * @param popupLeft the popupLeft to set
	 */
	public void setPopupLeft(String popupLeft) {
		this.popupLeft = popupLeft;
	}

	/**
	 * @return the popupTop
	 */
	public String getPopupTop() {
		return popupTop;
	}

	/**
	 * @param popupTop the popupTop to set
	 */
	public void setPopupTop(String popupTop) {
		this.popupTop = popupTop;
	}

	/**
	 * @return the caption
	 */
	public String getCaption() {
		return caption;
	}

	/**
	 * @param caption the caption to set
	 */
	public void setCaption(String caption) {
		this.caption = caption;
	}

	/**
	 * @return the paramName
	 */
	public String getParamName() {
		return paramName;
	}

	/**
	 * @param paramName the paramName to set
	 */
	public void setParamName(String paramName) {
		this.paramName = paramName;
	}

	/**
	 * @return the style
	 */
	public String getStyle() {
		return style;
	}

	/**
	 * @param style the style to set
	 */
	public void setStyle(String style) {
		this.style = style;
	}

	public String getRemoteMethod() {
		return remoteMethod;
	}

	public void setRemoteMethod(String remoteMethod) {
		this.remoteMethod = remoteMethod;
	}

	@Override
	protected String makeStartTag() {
		String inputId = "person_picker_" + paramName;
		// Creo la tabla
		Table table = new Table();
    	table.setCellPadding(0);
    	table.setCellSpacing(0);
    	Row innerRow = table.addRow();
    	Cell innerCell1 = innerRow.addCell();
    	Cell innerCell2 = innerRow.addCell();
    	// Creo la div
    	Div innerDiv = new Div();
    	
    	if (style!=null) {
    		innerDiv.setStyle(style);    		
    	}
    	if (styleClase!=null) {
    		innerDiv.setClass(styleClase);    		
    	}
    	innerDiv.setID(inputId);
    	innerCell1.addElement(innerDiv);
    	// Este es el input donde se guarda el valor, ya que en pantalla se muestra con una div. 
    	Input hidden = new Input();
        hidden.setType(Input.HIDDEN);
        hidden.setName(paramName);
        hidden.setValue("");
        hidden.setID(paramName);
        // Toma la persona por defecto de la request.
        String personId =  (String)this.getRequest().getAttribute(paramName);
        // Si la el id no es nulo lo agrego a la div.
        if (StringUtils.isNullOrBlank(personId) == false) {
        	Person person = CoreServiceLocator.getPersonService().findPersonById(new Long(personId));
        	Font font = new Font();
        	font.setClass("stdFont");
        	font.addElement(person.getFirstName() + " " + person.getLastName());
        	hidden.setValue(personId);
        	innerDiv.addElement(font);
        }

        innerCell1.addElement(hidden);
        
	    String href = contextPath + "/admin/user/personSearchDialog.do?selectParam=" + paramName;

	    if (this.getRemoteMethod() == null) {
	    	this.setRemoteMethod("get.matching.people");
	    }

	    // Button to pick person
    	A pickLink = new A();
    	pickLink.setHref(href);
    	//String popupWindowId = paramName;
    	pickLink.setOnClick("javascript:personSearchDiv.showPersonSearchDiv('" + remoteMethod + "', 200, 50, 'Find Person', '" +
    			paramName + "');");
        // Creo el boton para que salga el recuadro.
    	IMG buttonImage = new IMG();
    	
    	buttonImage.setSrc("/hrs-intranet/dynagraphic/button.jpg?code=button.date.pick&caption=Select");
    	buttonImage.setAlt("Pick Person");
    	buttonImage.setBorder(0);
    	buttonImage.setClass("imageButton");
    	buttonImage.setOnClick("javascript:personSearchDiv.showPersonSearchDiv(" +
    			"'" + remoteMethod + "', " +
    			popupLeft + ", " +
    			popupTop + ", '" +
    			caption + "', '" +
    			paramName + "');");

    	innerCell2.addElement(buttonImage);
    	
    	return table.toString();

	}
	
	private void createCells(Row row) {
		labelCell = row.addCell();
    	labelDiv = new Div();
    	labelDiv.setID("divlabel_" + paramName);
    	labelDiv.setClass("formLabelDiv");
    	labelDiv.setStyle("position:relative;left:3px");
    	labelDiv.addElement(caption);
        labelCell.addElement(labelDiv);
		
		valueCell = row.addCell();
    	valueDiv = new Div();
    	valueDiv.setID("div_" + paramName);
        valueCell.addElement(valueDiv);
	}

}
